<?php
/**
 * Created by PhpStorm.
 * User: 张良
 * Date: 2018/1/24
 * Time: 17:40
 */

namespace App\Http\Controllers\User;

use App\Http\Controllers\CommonController;
use Illuminate\Support\Facades\Input;
use Mockery\Exception;
use Request;
use DB;
use Redirect;
class RegisterController extends CommonController
{
    private $SMS_ID="SMS_129755168";


   public function register_index(){
   return view('user/register/register-index');
}
    public function ajaxCheckTel(){
        $input=Request::all();
        $tel=$input['tel'];
        $res=object_to_array(DB::table('user')->where(array("phone"=>$tel))->first());
        if($res){
         $flag=1; //1 手机号已存在
        }else{
         $flag=0; //0 手机号可用
        }
        return json_encode($flag, JSON_UNESCAPED_UNICODE);
    }
    public function get_tel_code(){
        $input=Request::all();
        $data=[
            "phoneNumber"=>$input['phoneNumber']
        ];
        sendSMS($data,$this->SMS_ID);
    }
    public function ajaxCheckAccount(){
        $input=Request::all();
        $account=$input['account'];
        $res=object_to_array(DB::table('user')->where(array("account"=>$account))->first());
        if($res){
            $flag=1; //1 用户名已存在
        }else{
            $flag=0; //0 用户名可用
        }
        return json_encode($flag, JSON_UNESCAPED_UNICODE);
    }
    public function register_account(){
        $input=Request::all();
        if(empty($input['tel'])){
            return back()->with('error','数据传输错误，请联系管理员');
        }else{
        $tel=$input['tel'];
        }
//        dump($tel);exit();
        return view('user/register/register-account')->with('data',$tel);
    }
    public function register_password(){
        $input=Request::all();
        $dir = 'Uploads/userPic/';
        $file = Input::file('file');
        $filename = time() . '_' . mt_rand() . "." . $file->getClientOriginalExtension();
        if (!$file->move($dir, $filename)) {
            return back()->with('error', '导入失败');
        }
        $filePath     = $dir . $filename;
        if(!empty($filePath)){
            $input['file']=$filePath;
        }
//        dump($input);exit();
        return view('user/register/register-password')->with('data',$input);
    }
    public function doRegister(){
        $input=Request::all();
        $data['account']=$input['account'];
        $data['review']=0;
        $data['type']=$input['type'];
        $data['password']=md5($input['pwd']);
        $data['phone']=$input['tel'];
        $data['created_at']=date('Y-m-d h:i:s');
        $data['updated_at']=date('Y-m-d h:i:s');
        $data['email']=$input['email'];
        $data['address']=$input['address'];
        $data['company']=$input['company'];
        $data['certificate_number']=$input['code_num'];
        $data['certificate_photo']=$input['file'];
        $data['real_name']=$input['name'];
        if(!empty($data)){
        $res=DB::table('user')->insert($data);
            if($res){
                return Redirect::to('/');
            }else{
                return back()->with('注册失败，请联系管理员');
            }
        }else{
           return back()->with('数据传输有误，请联系管理员');
        }
    }

}